package com.arges.web.serviceImpl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.arges.web.common.DateUtil;
import com.arges.web.dao.FunDao;
import com.arges.web.dao.GenericDao;
import com.arges.web.dao.WorkDao;
import com.arges.web.domain.bean.FunBean;
import com.arges.web.domain.pojo.Fun;
import com.arges.web.domain.pojo.Pat;
import com.arges.web.domain.pojo.Work;
import com.arges.web.page.PageInfo;
import com.arges.web.page.WebUtils;
import com.arges.web.service.FunManager;
import com.arges.web.service.PatManager;

/**
 * 患者随访记录业务层
 *
 * @author zxj
 *
 */
@Service
public class FunManagerImpl extends GenericManageImpl<Fun, FunBean> implements FunManager
{
	private Logger LOGGER = LoggerFactory.getLogger(FunManagerImpl.class);

	@Autowired
	private FunDao funDao;

	@Autowired
	private WorkDao workDao;

	@Autowired
	private PatManager patManager;

	@Override
	public GenericDao<Fun, Long> getGenericDao()
	{
		return funDao;
	}

	@Override
	public PageInfo<FunBean> getPageFun(PageInfo<Fun> pageInfo, String mrn, String startTime, String endTime)
	{
		PageInfo<FunBean> beanPageList = null;
		try
		{
			Criterion condiition = null;
			if (StringUtils.isNotBlank(mrn.trim()))
			{
				condiition = Restrictions.like("mrn", mrn, MatchMode.ANYWHERE);
			}
			if (StringUtils.isNotBlank(startTime) && (StringUtils.isNotBlank(endTime)))
			{
				Date startDate = DateUtil.convertString2Date(startTime, DateUtil.DEFAULT_DATE_TIME);
				Date endDate = DateUtil.convertString2Date(endTime, DateUtil.DEFAULT_DATE_TIME);
				Criterion timeCriterion = null;
				timeCriterion = Restrictions.between("f_date", startDate, endDate);
				if (null == condiition)
				{
					condiition = timeCriterion;
				}
				else
				{
					condiition = Restrictions.and(condiition, timeCriterion);
				}
			}
			else if (StringUtils.isNotBlank(startTime))
			{
				Date startDate = DateUtil.convertString2Date(startTime, DateUtil.DEFAULT_DATE_TIME);
				Criterion timeCriterion = null;
				timeCriterion = Restrictions.ge("f_date", startDate);
				if (null == condiition)
				{
					condiition = timeCriterion;
				}
				else
				{
					condiition = Restrictions.and(condiition, timeCriterion);
				}
			}
			else if (StringUtils.isNotBlank(endTime))
			{
				Date endDate = DateUtil.convertString2Date(endTime, DateUtil.DEFAULT_DATE_TIME);
				Criterion timeCriterion = null;
				timeCriterion = Restrictions.le("f_date", endDate);
				if (null == condiition)
				{
					condiition = timeCriterion;
				}
				else
				{
					condiition = Restrictions.and(condiition, timeCriterion);
				}
			}
			PageInfo<Fun> pojoPageList = funDao.findAll(pageInfo, Fun.class, condiition);

			beanPageList = WebUtils.pojoToBeanPage(pojoPageList, FunBean.class);

			List<Work> workList = workDao.findAll();
			List<Pat> patList = patManager.findAll();
			for (FunBean funBean : beanPageList.getResult())
			{
				for (Pat pat : patList)
				{
					if (pat.getMrn().equals(funBean.getMrn()))
					{
						funBean.setP_name(pat.getName());
					}
				}
				for (Work work : workList)
				{
					if (work.getEno().equals(funBean.getEno()))
					{
						funBean.setD_name(work.getD_name());
					}
				}
			}
		}
		catch (Exception e)
		{
			return null;
		}
		return beanPageList;
	}

	@Override
	public FunBean findFunById(long parseLong)
	{
		Fun fun = funDao.get(parseLong);
		FunBean funBean = null;
		if (null != fun)
		{
			funBean = new FunBean();
			BeanUtils.copyProperties(fun, funBean);
		}
		return funBean;
	}

	@Override
	public void deleteByIds(String ids)
	{
		String[] split = ids.split(",");
		Long[] deleteId = new Long[split.length];
		for (int i = 0; i < split.length; i++)
		{
			deleteId[i] = Long.valueOf(split[i]);
		}
		funDao.deletePatByIds(deleteId);

	}

}
